package com.zjq.springsocial.renren.android;

import org.springframework.security.crypto.encrypt.AndroidEncryptors;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.sqlite.SQLiteConnectionRepository;
import org.springframework.social.connect.sqlite.support.SQLiteConnectionRepositoryHelper;
import org.springframework.social.connect.support.ConnectionFactoryRegistry;

import android.app.Application;
import android.database.sqlite.SQLiteOpenHelper;

import com.zjq.springsocial.renren.api.Renren;
import com.zjq.springsocial.renren.connect.RenrenConnectionFactory;

/**
 * 
 * <P>
 * 人人网appliction类
 * </P>
 * 
 * @author zhongjingquan
 * 
 * @version 0.0.1-SNAPSHOT 2013-7-18下午2:58:10
 */
public class RenrenAppliction extends Application {

	private ConnectionFactoryRegistry connectionFactoryRegistry;
	private SQLiteOpenHelper repositoryHelper;
	private ConnectionRepository connectionRepository;

	// ***************************************
	// Application Methods
	// ***************************************

	@Override
	public void onCreate() {
		// create a new ConnectionFactoryLocator and populate it with Facebook
		// ConnectionFactory
		this.connectionFactoryRegistry = new ConnectionFactoryRegistry();
		this.connectionFactoryRegistry.addConnectionFactory(new RenrenConnectionFactory(getRenrenAppId(), getRenrenAppSecret()));
		
		// set up the database and encryption
		this.repositoryHelper = new SQLiteConnectionRepositoryHelper(this);
		this.connectionRepository = new SQLiteConnectionRepository(this.repositoryHelper,
				this.connectionFactoryRegistry, AndroidEncryptors.text("password", "5c0744940b5c369b"));
	}
	
	// ***************************************
	// Private methods
	// ***************************************
	private String getRenrenAppId(){
		return getString(R.string.renren_app_id);
	}

	private String getRenrenAppSecret(){
		return getString(R.string.renren_app_secret);
	}

	// ***************************************
	// Public methods
	// ***************************************
	public ConnectionRepository getConnectionRepository() {
		return this.connectionRepository;
	}

	public RenrenConnectionFactory getRenrenConnectionFactory() {
		return (RenrenConnectionFactory) this.connectionFactoryRegistry.getConnectionFactory(Renren.class);
		}
}
